using System;
using System.Data;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
namespace DAO
{
    public class LoaiDocGia : Interface
    {
        string s = Application.StartupPath;
        public List<DTO.LoaiDocGia> DanhSachLoaiDocGia()
        {
            List<DTO.LoaiDocGia> l = new List<DTO.LoaiDocGia>();
            Provider prv = new Provider("Server=.\\SQLEXPRESS;AttachDbFilename=" + s + "\\QLTV.mdf; Trusted_Connection=Yes;Integrated security=true;User Instance=true");
            DataTable dt = prv.ExecuteQuery("SELECT * FROM LoaiDocGia");
            if(dt == null)
            {
                Provider prv1 = new Provider("Server=.\\;AttachDbFilename=" + s + "\\QLTV.mdf; Trusted_Connection=Yes;Integrated security=true;User Instance=true");
                dt = prv1.ExecuteQuery("SELECT * FROM LoaiDocGia");
            }
            DTO.LoaiDocGia ldg;
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                ldg = new DTO.LoaiDocGia((int)dt.Rows[i]["MaLoaiDocGia"], dt.Rows[i]["TenLoaiDocGia"].ToString());
                l.Add(ldg);
            }
            return l;
        }
        public int LayMaLoaiDocGia(string tenLDG)
        {
            Provider prv = new Provider("Server=.\\SQLEXPRESS;AttachDbFilename=" + s + "\\QLTV.mdf; Trusted_Connection=Yes;Integrated security=true;User Instance=true");
            DataTable dt = prv.ExecuteQuery("SELECT * FROM LoaiDocGia");
            if (dt == null)
            {
                Provider prv1 = new Provider("Server=.\\;AttachDbFilename=" + s + "\\QLTV.mdf; Trusted_Connection=Yes;Integrated security=true;User Instance=true");
                dt = prv1.ExecuteQuery("SELECT * FROM LoaiDocGia");
            }

            for (int i = 0; i < dt.Rows.Count; i++)
            {
                if (dt.Rows[i]["TenLoaiDocGia"].ToString().Trim() == tenLDG.Trim())
                    return (int)dt.Rows[i]["MaLoaiDocGia"];
            }
            return -1;
        }
    }
}
